﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using eDonkeyAPI.Models;

namespace eDonkeyAPI.Controllers
{
    public class TeacherController : ApiController
    {
        private mydbEntities db = new mydbEntities();

        // GET api/Courses
        public IEnumerable<Teacher> GetTeachers()
        {
            return db.Teachers.AsEnumerable();
        }

        // GET api/Teacher/5
        public Teacher GetTeacher(int id)
        {
            Teacher teacher = db.Teachers.Where(x => x.Id == id).FirstOrDefault();
            teacher.Courses = db.Courses.Where(c => c.Teacher.Id == id).ToArray();

            if (teacher == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return teacher;
        }

        // PUT api/Teacher/5
        public HttpResponseMessage PutTeacher(int id, Teacher teacher)
        {
            if (ModelState.IsValid && id == teacher.Id)
            {
                db.Entry(teacher).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }


        }

        // POST api/Teacher
        public HttpResponseMessage PostTeacher(Teacher teacher)
        {
            if (ModelState.IsValid)
            {
                db.Teachers.Add(teacher);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, teacher);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = teacher.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Teacher/5
        public HttpResponseMessage DeleteTeacher(int id)
        {
            Teacher teacher = db.Teachers.Find(id);
            if (teacher == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Teachers.Remove(teacher);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, teacher);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}